using System.Runtime.InteropServices;
using System.Text;

namespace TeamTasker.Utils {
    /// <summary>
    /// Read/Write operations for ini file.
    /// 
    /// TODO: remove state from this class. Either:
    ///     a) add "file name" param to all statis methods or
    ///     b) delete all static methods and provide file name to class constructor;
    /// 
    /// </summary>
    public class IniFile {
        private static string m_FileName = null;

        // Path to default ini file
        public static string CurrentFileName {
            get { return m_FileName; }
            set { m_FileName = value; }
        }

        [DllImport("kernel32", EntryPoint = "WritePrivateProfileStringA", ExactSpelling = true, CharSet = CharSet.Ansi,
            SetLastError = true)]
        private static extern int WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString,
                                                            string lpFileName);

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileStringA", ExactSpelling = true, CharSet = CharSet.Ansi,
            SetLastError = true)]
        private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault,
                                                          StringBuilder lpReturnedString, int nSize, string lpFileName);

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileIntA", ExactSpelling = true, CharSet = CharSet.Ansi,
            SetLastError = true)]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault,
                                                       string lpFileName);

        [DllImport("kernel32", EntryPoint = "GetLastError", ExactSpelling = true, CharSet = CharSet.Ansi,
            SetLastError = true)]
        private static extern int GetLastError();

        public static void SetStringValue(string Section, string Key, string value) {
            SetStringValue(Section, Key, value, null);
        }


        public static void SetStringValue(string Section, string Key, string value, string FileName) {
            if (FileName == null) {
                FileName = m_FileName;
            }

            WritePrivateProfileString(Section, Key, value, FileName);
        }

        public static string GetStringValue(string Section, string Key, string DefaultValue) {
            return GetStringValue(Section, Key, DefaultValue, null);
        }

        public static string GetStringValue(string Section, string Key) {
            return GetStringValue(Section, Key, "", null);
        }

        public static string GetStringValue(string Section, string Key, string DefaultValue, string FileName) {
            if (FileName == null) {
                FileName = m_FileName;
            }

            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(Section, Key, DefaultValue, sb, sb.Capacity, FileName);

            return sb.ToString();
        }
        
        public static int GetIntValue(string Section, string Key, int DefaultValue) {
            return GetIntValue(Section, Key, DefaultValue, null);
        }

        public static int GetIntValue(string Section, string Key) {
            return GetIntValue(Section, Key, 0, null);
        }
        
        public static int GetIntValue(string Section, string Key, int DefaultValue, string FileName) {
            if (FileName == null) {
                FileName = m_FileName;
            }
            return GetPrivateProfileInt(Section, Key, DefaultValue, FileName);
        }
     
    }
}